<?php

namespace App\Http\Controllers;

use App\Models\Goods;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GoodsController extends Controller
{
    public function index(Request $request)
    {
        $query = Goods::with(['user'])->orderBy('sort', 'desc');
        if ($request->input('category_id')) {
            $query = $query->where('category_id', $request->input('category_id'));
        }
        if ($request->input('status')) {
            $query = $query->where('status', $request->input('status'));
        }
        if ($request->input('keyword')) {
            $query = $query->where('title', 'like', '%' . $request->input('keyword') . '%');
        }
        $data = $query->paginate();
        return $this->response($data);
    }

    public function source($id)
    {
        $data = Goods::with(['user'])->where('source_id', $id)->orWhere('id', $id)->orderBy('created_at', "asc")->get();
        return $this->response($data);
    }

    public function collect(Request $request)
    {
        $query = Goods::orderBy('sort', 'desc');
        if ($request->input('category_id')) {
            $query = $query->where('category_id', $request->input('category_id'));
        }
        if ($request->input('status')) {
            $query = $query->where('status', $request->input('status'));
        }
        $data = $query->paginate();
        return $this->response($data);
    }

    public function storeCollect(Request $request)
    {
        $goods = Goods::where('id', $request->input('goods_id'))->first();
        $obj = new Goods();
        $obj->fill([
            'source_id' => $goods->source_id ? $goods->source_id : $goods->id,
            'title' => $goods->title,
            'user_id' => $request->user()->id,
            'property' => $goods->property,
            'category_id' => $goods->category_id,
            'price' => $goods->price,
            'pic' => $goods->pic,
            'content' => $goods->content,
            'status' => $goods->status,
        ]);
        $obj->save();
        return $this->response($obj->id);
    }

    public function hot()
    {
        $obj = Goods::orderBy('hot', 'desc')->select('id', 'title', 'hot')->limit(10)->get();
        return $this->response($obj);
    }

    public function view($id)
    {
        $obj = Goods::with(['user'])->find($id);
        return $this->response($obj);
    }

    public function scan()
    {
        $obj = Goods::with(['user'])->first();
        return $this->response($obj);
    }

    public function store(Request $request, Goods $obj)
    {
        $all = $request->all();
        $obj->fill($all);
        $obj->user_id = $request->user()->id;
        $obj->qrcode = $this->createQrcode($obj->id);
        $obj->save();
        return $this->response($obj->id);
    }

    private function createQrcode($goods_id)
    {
        $app = Factory::officialAccount(config('wechat'));
        $result = $app->qrcode->forever(56);
        $url = $app->qrcode->url($result['ticket']);
        $content = file_get_contents($url);
        $path = "/qrcode/" . $goods_id . '.png';
        Storage::disk('public')->put($path, $content);
        return Storage::disk('public')->url($path);
    }

    public function update($id, Request $request)
    {
        $obj = Goods::find($id);
        $data = $request->all();
        $data['status'] = 1;
        $obj->update($data);
        return $this->response($obj->id);
    }

    public function destroy($id)
    {
        $obj = Goods::find($id);
        $obj->delete();
        return $this->response();
    }
}
